package controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.Blog;
import model.BlogDao;
import model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

/*
*通过这个类，来实现一些后端提供的接口
* */
@WebServlet("/blog")
public class BlogServlet extends HttpServlet {
    //实例化一个ObjectMapper对象，用来将数据转换为json格式
    private ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BlogDao blogDao = new BlogDao();
        String blogId = req.getParameter("blogId");
        //从query string中查询一下看是否有blogId,如果有就认为是查询指定博客；如果没有就是查询所有博客。
        if(blogId == null){
            List<Blog> blogs = blogDao.selectAll();
            //将从数据库中查询到的数据转换为json格式
            String respString = objectMapper.writeValueAsString(blogs);
            //使客户端浏览器区分不同种类的数据，并根据不同的MIME调用浏览器内不同的程序嵌入模块来处理相应的数据。
            resp.setContentType("application/json;charset=utf8");
            resp.getWriter().write(respString);
        }else{
            //上述使用getParameter从query string中得到的blogId是一个字符串，这里需要将其转为数字
            Blog blog = blogDao.selectOne(Integer.parseInt(blogId));
            String respString = objectMapper.writeValueAsString(blog);
            resp.setContentType("application/json;charset=utf8");
            resp.getWriter().write(respString);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        //1.先从请求中拿到标题和正文
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        if(title == null || title.equals("") || content == null || content.equals("")){
            //只要其中有一个符合条件进入判断，就认为内容是不全的。没有标题或者内容，博客发布失败
            String html = "<h3>title 或者 content 为空！新增博客是失败！</h3>";
            resp.setContentType("text/html;charset=uft8");
            resp.getWriter().write(html);
            return;
        }
        //2.从会话中拿到作者的id
       HttpSession session = req.getSession(false);
        if(session == null){
            String html = "<h3>当前用户未登录！新增博客是失败！</h3>";
            resp.setContentType("text/html;charset=uft8");
            resp.getWriter().write(html);
            return;
        }
        User user = (User)session.getAttribute("user");
        if(user == null){
            String html = "<h3>当前用户未登录！新增博客是失败！</h3>";
            resp.setContentType("text/html;charset=uft8");
            resp.getWriter().write(html);
            return;
        }
        //3.构造Blog对象
        Blog blog = new Blog();
        blog.setUserId(user.getUserId());
        blog.setTitle(title);
        blog.setContent(content);
        blog.setPostTime(new Timestamp(System.currentTimeMillis()));
        //4.插入blog对象到数据库中
        BlogDao blogDao = new BlogDao();
        blogDao.insert(blog);
        //5.跳转到博客列表页(重定向)
        resp.sendRedirect("blog_list.html");
    }
}
